<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>

<form id="itemTableForm">

</form>
<div class="panel-body" id="LeftDiv">
    <section class="main-content col-lg-12">
        <div class="table-responsive no-border">
            <div class="datatable-top">
                <div class="pull-left">
                    <form:form id="idForm" modelAttribute="inpBillDetail"
                               onsubmit="return loadDivForm('rigthDoctorCenterDiv','idForm','${ctx}/finance/inpBillDetail/refundList');"
                               method="post" class="form-inline">
                        <input type="hidden" name="patientId" value="${inpBillDetail.patientId}">
                        <input type="hidden" name="visitId" value="${inpBillDetail.visitId}">
                        起始时间：<input id="startDate" data-parsley-required="true" name="startDate" value="${inpBillDetail.startDate}" onclick="WdatePicker({dateFmt:'yyyy-MM-dd',isShowClear:false});" class="form-control Wdate">
                        结束时间：<input type="endDate" id="endDate" data-parsley-required="true" name="endDate" value="${inpBillDetail.endDate}" onclick="WdatePicker({dateFmt:'yyyy-MM-dd',isShowClear:false});" class="form-control Wdate">
                        项目：<input class="form-control" id="name" name="name" value="${inpBillDetail.name}" type="text" />
                        <input id="btnSubmit" class="btn btn-primary" type="submit" value="查询"/></li>
                        <input class="btn btn-new"
                               onclick="retAmountAll('${inpBillDetail.patientId}','${inpBillDetail.visitId}')"
                               type="button" value="退费" id="retAmountBut"/>
                    </form:form>
                </div>
            </div>
            <table id="itemTableId" class="table table-bordered mg-t datatable" checkbox="true">
                <thead>
                <tr>
                    <th><input type="checkbox" id="retAmount"></th>
                    <th style="display:none">patientId</th>
                    <th style="display:none">visitId</th>
                    <th style="display:none">priceId</th>
                    <th style="display:none">storageId</th>
                    <th style="display:none">applId</th>
                    <th>类别</th>
                    <th>项目名称</th>
                    <th>项目代码</th>
                    <th>规格</th>
                    <th>单位</th>
                    <th>数量</th>
                    <th>应付费用</th>
                    <th>实收费用</th>
                    <th style="color: red">申请退费数量</th>
                    <th style="display:none">ordersId</th>
                    <th style="display:none">inpBillId</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <c:forEach items="${list}" var="list" varStatus="count">
                    <%--<tr onclick="addSubProjectDiv('treatProjectItem','${ctx}/treatProjectItem/treatProjectItem/indexTreat?type=3&id=${doctorTempla.id}')">--%>
                <tr onclick="">
                    <td><input type="checkbox" value="${list.id}" name="inpBillId"></td>
                    <td data-param="patientId" data-value="${list.patientId}"
                        style="display:none">${list.patientId}</td>
                    <td data-param="visitId" data-value="${list.visitId}" style="display:none">${list.visitId}</td>
                    <td data-param="priceId" data-value="${list.priceId}" style="display:none">${list.priceId}</td>
                    <td data-param="storageId" data-value="${list.storageId}"
                        style="display:none">${list.storageId}</td>
                    <td data-param="id"
                        data-value="<c:if test="${list.applyDrugWithdra.id == '' || list.applyDrugWithdra.id == null}">1</c:if>"
                        style="display:none">${list.applyDrugWithdra.id}</td>
                    <td data-param="itemClass" data-value="${list.itemClass}"><input type="hidden"
                                                                                     value="${list.itemClass}">
                        <c:choose>
                            <c:when test="${list.itemClass == 'A'}">
                                西药
                            </c:when>
                            <c:when test="${list.itemClass == 'B'}">
                                中药
                            </c:when>
                            <c:when test="${list.itemClass == 'C'}">
                                检验
                            </c:when>
                            <c:when test="${list.itemClass == 'D'}">
                                检查
                            </c:when>
                            <c:when test="${list.itemClass == 'E'}">
                                治疗
                            </c:when>
                            <c:when test="${list.itemClass == 'F'}">
                                手术
                            </c:when>
                            <c:when test="${list.itemClass == 'G'}">
                                麻醉
                            </c:when>
                            <c:when test="${list.itemClass == 'H'}">
                                血费
                            </c:when>
                            <c:when test="${list.itemClass == 'I'}">
                                材料
                            </c:when>
                            <c:otherwise>
                                其他
                            </c:otherwise>
                        </c:choose>
                    </td>
                    <td data-param="itemName" data-value="${list.itemName}"> ${list.itemName}</td>
                    <td data-param="itemCode" data-value="${list.itemCode}"> ${list.itemCode}</td>
                    <td data-param="itemSpec" data-value="${list.itemSpec}"> ${list.itemSpec}</td>
                    <td data-param="units" data-value="${list.units}"><input type="hidden"
                                                                             value="${list.units}"> ${fns:getDictLabels(list.units,'SPEC_UNIT','')}
                    </td>
                    <td data-param="amount" data-value="${list.amount}">${list.amount}</td>
                    <td data-param="costs" data-value="${list.costs}"> ${list.costs}</td>
                    <td data-param="charges" data-value="${list.charges}"> ${list.charges}</td>
                    <td data-param="retAmount" data-value="1" style="color: red"><input type="number"
                                                                                        <c:if test="${list.applyDrugWithdra.retType == '1'}">readonly="true" </c:if>
                                                                                        onchange="selectAmount(this)"
                                                                                        value="${list.applyDrugWithdra.retAmount}">
                    </td>
                    <td style="display:none" data-param="ordersId" data-value="${list.orders.id}"
                        style="color: red">${list.orders.id}</td>
                    <td style="display:none" data-param="inpBillId" data-value="${list.id}"
                        style="color: red">${list.id}</td>
                    <td>
                        <c:choose>
                            <c:when test="${list.applyDrugWithdra.retType == '0' || list.applyDrugWithdra.retType == null}">
                                <a href="#"
                                   onclick="applyForWithdrawal(this,'LeftDiv','${ctx}/finance/inpBillDetail/refundList?patientId=${inpBillDetail.patientId}&visitId=${inpBillDetail.visitId}')"
                                   class="btn btn-success btn-xs">申请退药</a>
                            </c:when>
                            <c:otherwise>
                                已申请，请等待
                            </c:otherwise>
                        </c:choose>

                    </td>
                </tr>
                </c:forEach>
            </table>
            <%--<div class="pagination">${page}</div>--%>
        </div>
    </section>
</div>


<script>

    function retAmountAll(patientId, visitId) {
        var ordersIds = new Array();
        var isOk = 0;
        var itemTable = $("#itemTableId tbody tr").find('td').eq(0).find('input');
//        alert(itemTable.length);
//        $("#itemTableId tbody tr").each(function (i,element ) {
//            for(){
//            }
        var text = "";
        $('input[name="inpBillId"]:checked').each(function (i, e) {

            var inpBillId = $(this).val();
            if (inpBillId == '' || inpBillId == null) {
                toastr.error("请选择需要退费的项目");
            } else {
                var row = $(this).parent().parent();
                var amount = Number($(row).find('td').eq(11).text());
                var retAmount = $(row).find('td').eq(14).find("input").val();
                isOk = isAdopt(retAmount, amount);

                var patientId = $(row).find("td").eq(1).text();
                var visitId = $(row).find("td").eq(2).text();
                var priceId = $(row).find("td").eq(3).text();
                var storageId = $(row).find("td").eq(4).text();
                var itemClass = $(row).find("td").eq(6).find("input").val();
                var itemName = $(row).find("td").eq(7).text();
                var itemCode = $(row).find("td").eq(8).text();
                var itemSpec = $(row).find("td").eq(9).text();
                var units = $(row).find("td").eq(10).find("input").val();
                var costs = $(row).find("td").eq(12).text();
                var charges = $(row).find("td").eq(13).text();
                var ordersId = $(row).find("td").eq(15).text();
                var inpBillId = $(row).find("td").eq(16).text();

                text += "<tr><td>" +
                "<input type='hidden' name='list[" + i + "].amount'" + " value='" + amount + "'/>" +
                "<input data-parsley-validate='true' type='hidden' name='list[" + i + "].retAmount'" + " value='" + retAmount + "'/>" +
                "<input type='hidden' name='list[" + i + "].patientId'" + " value='" + patientId + "'/>" +
                "<input type='hidden' name='list[" + i + "].visitId'" + " value='" + visitId + "'/>" +
                "<input type='hidden' name='list[" + i + "].priceId'" + " value='" + priceId + "'/>" +
                "<input type='hidden' name='list[" + i + "].storageId'" + " value='" + storageId + "'/>" +
                "<input type='hidden' name='list[" + i + "].itemClass'" + " value='" + itemClass + "'/>" +
                "<input type='hidden' name='list[" + i + "].itemName'" + " value='" + itemName + "'/>" +
                "<input type='hidden' name='list[" + i + "].itemCode'" + " value='" + itemCode + "'/>" +
                "<input type='hidden' name='list[" + i + "].itemSpec'" + " value='" + itemSpec + "'/>" +
                "<input type='hidden' name='list[" + i + "].units'" + " value='" + units + "'/>" +
                "<input type='hidden' name='list[" + i + "].costs'" + " value='" + costs + "'/>" +
                "<input type='hidden' name='list[" + i + "].charges'" + " value='" + charges + "'/>" +
                "<input type='hidden' name='list[" + i + "].ordersId'" + " value='" + ordersId + "'/>" +
                "<input type='hidden' name='list[" + i + "].inpBillId'" + " value='" + inpBillId + "'/>" +
                "</td></tr>";
            }
        });
        var list = "<table>" + text + "</table>";
        $("#itemTableForm").html(list);
        if(isOk == 1){
            toastr.error("退费数量大于已开数量");
            return;
        } else if(isOk == 2){
            toastr.error("退费数量不能等于0");
        } else {
            var divUrl = "${ctx}/finance/inpBillDetail/refundList?patientId=" + patientId + "&visitId=" + visitId;
            $("#itemTableForm").ajaxSubmit({
                type: 'post', // 提交方式 get/post
                url: '${ctx}/applydrugwithdra/saveAll', // 需要提交的 url
                success: function (data) { // data 保存提交后返回的数据，一般为 json 数据
                    if (data.code == "success") {
                        toastr.success(data.data);
                        loadDiv("rigthDoctorCenterDiv", divUrl);
                    } else if (data.code == 'info') {
                        loadDiv("rigthDoctorCenterDiv", divUrl);
                    } else {
                        flag = true;
                        toastr.error(data.data);
                    }
                }
            });
        }
    }


    /**
     * 退费数量
     * @param tr
     */
    function selectAmount(tr) {
        $(tr).parent().parent().addClass("table-selected");
        var row = $("#itemTableId .table-selected").find("td");
        var amount = Number(row.eq(11).text());
        var retAmount = Number($(tr).val());
        var isOk = isAdopt(retAmount, amount);
        if(isOk == 1){
            toastr.error("退费数量大于已开数量");
            return;
        } else if(isOk == 2){
            toastr.error("退费数量不能等于0");
            return;
        } else {
            $(tr).parent().attr("data-value", retAmount);
            $(tr).attr("value",retAmount);
        }
    }

    function isAdopt(retAmount, amount) {
        if (retAmount > amount) {
            return 1;
        } else if (retAmount == 0) {
            return 2;
        }
    }

    /**
     单行保存*
     * @param tr
     * @param div
     * @param divUrl
     */
    function applyForWithdrawal(tr, div, divUrl) {
        $(tr).parent().parent().addClass("table-selected");
        var row = $("#itemTableId .table-selected").find("td");
        var amount = Number($(tr).parent().parent().find('td').eq(11).text());
        var retAmount = Number($(tr).parent().parent().find('td').eq(14).find("input").val());
        var isOk = isAdopt(retAmount, amount);
        if(isOk == 1){
            toastr.error("退费数量大于已开数量");
            return false;
        } else if(isOk == 2){
            toastr.error("退费数量不能等于0");
            return false;
        } else {
            var applyDrugWithdra = '';
            for (var i = 0; i < row.length - 1; i++) {
                applyDrugWithdra += '&' + $(row[i]).data('param') + '=' + $(row[i]).data('value');
            }
            $.ajax({
                type: "post",
                url: '${ctx}/applydrugwithdra/save?' + applyDrugWithdra.substr(1),
                success: function (data) {
                    if (data.code == "success") {
                        toastr.success(data.data);
                        loadDiv(div, divUrl);
                    } else {
                        toastr.success(data.data);
                    }
                }
            })
        }

    }
</script>